package idv.takeshi.software.productline.bookstore.domain.model.shoppingcart;

import java.math.BigDecimal;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import idv.takeshi.software.productline.bookstore.domain.model.book.Book;

/**
 * A model class for representing as a item within a ShoppingCart.
 * @author takeshi
 *
 */
public class ShoppingCartItem {
 
	private int quantity;
	private Book book;
	
	/**
	 * Increment one quantity for this ShoppingCartItem. 
	 */
	public void incrementQuantity(){
		this.quantity++;
	}

	/**
	 * @return the quantity
	 */
	public int getQuantity() {
		return quantity;
	}
	
	/**
	 * Calculate Price for this shoppingCartItem.
	 * @return
	 */
	public BigDecimal getTotalPrice(){
		return book.getUnitPrice().multiply(new BigDecimal(this.quantity));
	}

	/**
	 * @return the book
	 */
	public Book getBook() {
		return book;
	}

	/**
	 * @param quantity the quantity to set
	 */
	protected void setQuantity(int quantity) {
		if(quantity <= 0)
			throw new UpdateQuantityOfShoppingCartItemException("quantity:" + quantity +
					" shall not small than or equals to 0");
		this.quantity = quantity;
	}

	/**
	 * @param book the book to set
	 */
	protected void setBook(Book book) {
		this.book = book;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj == null) { return false; }
		   if (obj == this) { return true; }
		   if (!(obj instanceof ShoppingCartItem)) {
		     return false;
		   }
		   ShoppingCartItem rhs = (ShoppingCartItem) obj;
		   return new EqualsBuilder()
			   .append(this.quantity, rhs.quantity)
			   .append(this.book, rhs.book)
		       .isEquals();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		return new HashCodeBuilder(61, 97).
	       append(this.quantity).
	       append(this.book).
	       toHashCode();
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return ReflectionToStringBuilder.toString(this, ToStringStyle.MULTI_LINE_STYLE);
	}
	
	
	 
}
 
